var jkd = {
    setBtnStatus: function () {
        var row = $('#grid_jkd').datagrid("getSelected");
        if (row) {
            if (row.zt == 'ybc') {   //被选择的借款单  已保存
                $("#btn_jkd_mod").show();
                $("#btn_jkd_del").show();
                $("#btn_jkd_tj").show();
            }
            if (row.zt == 'ytj' && row.yfje == 0) {   //被选择的借款单  已提交
                $("#btn_jkd_mod").hide();
                $("#btn_jkd_del").show();
                $("#btn_jkd_tj").hide();
            }
            if (row.zt == 'ysp' && row.yfje == 0) {   //被选择的借款单  已审批
                $("#btn_jkd_mod").hide();
                $("#btn_jkd_del").show();
                $("#btn_jkd_tj").hide();
            }
        } else {
            $("#btn_jkd_mod").hide();
            $("#btn_jkd_del").hide();
            $("#btn_jkd_tj").hide();
        }
    }
}


$(function () {
    $('#grid_jkd').datagrid({
        url: $.webPath() + "/jhgl/jkd/jkdList",
        toolbar: "#toolbar_jkd",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        pageNumber: 1,
        collapsible: true,
        fit: true,
        fitColumns: true,
        columns: [
            [
                {title: '付款日期', field: 'zfsj', align: 'center', width: 74},
                {title: '借款单号', field: 'jkdbm', align: 'center', width: 99, formatter: jkdCell},
                {title: '供应商', field: 'gys_jc', align: 'center', width: 119},
                {title: '支付款项', field: 'zfkx', align: 'center', width: 74},
                {title: '支付方式', field: 'zffs_z', align: 'center', width: 51},
                {title: '借款金额', field: 'zfje', align: 'right', formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }, width: 168},
                {title: '已付款', field: 'yfje', align: 'right', formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }, width: 167},
                {title: '借款公司', field: 'jkgs_jc', align: 'center', width: 65},
                {title: '申报公司', field: 'sbgs_jc', align: 'center', width: 70},
                {title: '申报部门', field: 'sbbm_jc', align: 'center', width: 59},
                {title: '借款人', field: 'createUser_xm', align: 'center', width: 36},
                {title: '申请日期', field: 'dateCreated', align: 'center', width: 65},
                {title: '状态', field: 'zt', width: 48, align: 'center',
                    formatter: formatJkdZT1
                }
            ]
        ],
        onLoadSuccess: function () {
            var rows = $('#grid_jkd').datagrid("getRows");
            if (rows) {
                $('#grid_jkd').datagrid("selectRow", 0);
            }
            jkd.setBtnStatus();

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onSelect: function (rowIndex, rowData) {
            jkd.setBtnStatus();
        }
    });

    $("#btn_jkd_mod").click(function () {
        jkd_modefy('修改借款单', 'update_jkd');
    });

    $("#btn_jkd_del").click(function () {
        jkd_delete();
    });
    $("#btn_jkd_tj").click(function () {
        jkd_tj();
    });

    //查询按钮
    $("#btn_jkd_search").click(function () {
        jkd_search();
    });


});

//修改票据
function jkd_modefy(title, action) {
    var row = $("#grid_jkd").datagrid("getSelected");
    if (row) {
        if (row.zt != 'ybc') {
            $.messager.alert("提示", "请选择已保存的借款单!", "warning");
            return;
        }
        var href = $.webPath() + "/jhgl/jkd/jkd_content?jkdId=" + row.id;
        var actionUrl = $.webPath() + "/jhgl/jkd/update_jkd";
        jkd_showDialog(title, href, actionUrl);
    } else {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
}

//弹出新增或修改的窗口
function jkd_showDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        },
        title: title,
        width: 750,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_jkd_content').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_jkd').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除借款单
function jkd_delete() {
    var row = $('#grid_jkd').datagrid('getSelected');
    if (row.yfje != 0) {
        $.messager.alert("提示", "请选择未付款的借款单!", "warning");
        return;
    }
    if (row) {
        $.messager.confirm('提示', '是否删除该借款单？', function (r) {
            if (r) {
                var index = $('#grid_jkd').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/jhgl/jkd/delete_jkd",
                    dataType: 'json',
                    type: 'POST',
                    data: {jkdId: row.id},
                    beforeSend: function () {
                        $("#layout_jkd").mask();
                    },
                    success: function (data) {
                        $("#layout_jkd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_jkd').datagrid('deleteRow', index);
                            $('#grid_jkd').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的借款单！", "warning");
    }
}


//提交借款单
function jkd_tj() {
    var row = $('#grid_jkd').datagrid('getSelected');
    if (row.zt != 'ybc') {
        $.messager.alert("提示", "请选择已保存的借款单提交!", "warning");
        return;
    }
    if (row) {
        $.messager.confirm('提示', '是否提交该借款单？', function (r) {
            if (r) {
                var index = $('#grid_jkd').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/jhgl/jkd/commit_jkd",
                    dataType: 'json',
                    type: 'POST',
                    data: {jkdId: row.id},
                    beforeSend: function () {
                        $("#layout_jkd").mask();
                    },
                    success: function (data) {
                        $("#layout_jkd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_jkd').datagrid('deleteRow', index);
                            $('#grid_jkd').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择提交的借款单！", "warning");
    }
}


function formatJkdZT1(val, row) {
    if (val == 'ybc')
        val = '已保存';
    if (val == 'ytj')
        val = '已提交';
    if (val == 'ysp')
        val = '已审批';

    if (val == '已保存') {
        return val;
    } else {
        return '<a href="javascript:viewJkdFlowPicturescl();" style="color: red">' + val + '</a>';
    }
}

function viewJkdFlowPicturescl() {
    var row = $('#grid_jkd').datagrid('getSelected');
    $.window({
        title: "流程历史",
        iconCls: 'icon-add2',
        url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)) + "&businessNo=" + row.jkdbm,
        width: 650,
        height: 500,
        closed: true,
        modal: true,
        isMax: true
    });


}

//查询借款单
function jkd_search() {
    var jkgsMc = $("#gsId").combotree('getText') == null ? null : $("#gsId").combotree('getText');
    var jkgsId = $("#gsId").combotree('getValue') == null ? null : $("#gsId").combotree('getValue');
    $('#grid_jkd').datagrid('load',
        {
            jkgsId: jkgsId,
            jkgsMc: jkgsMc,
            gysMc: $("#jkd_gysmc").val(),
            jkdbm: $("#jkd_jkdbm").val()
        }
    );
}
